//
//  JSMLeavingMainDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingMainDetailsViewController.h"
#import "JSMLeavingMoreMainDetailsModel.h"
#import "JSMLeavingUserModel.h"
#import "JSMLeavingMoreDetailsCommentsModel.h"
#import "JSMLeavingMoreDetailsTableViewCell.h"
#import "JSMLeavingMoreDetailsCollectionViewCell.h"
@interface JSMLeavingMainDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong) BaseTableView *moreMainDetailsTableView;
@property(nonatomic, strong) NSNumber *spot_id;
@property(nonatomic, strong) NSMutableArray *moreDetailsMarr;
@property(nonatomic, strong) NSMutableArray *recommendationsMarr;
@property(nonatomic, strong) NSMutableArray *commendMarr;
@end

@implementation JSMLeavingMainDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.spot_id = self.mainModel.spot_id;
    [self createMoreMainDetailsTableView];
    [self createTableHeaderView];
    [self createData];
}

- (void)createMoreMainDetailsTableView {
    self.moreMainDetailsTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 32) style:UITableViewStyleGrouped];
    [self.view addSubview:self.moreMainDetailsTableView];
    self.moreMainDetailsTableView.dataSource = self;
    self.moreMainDetailsTableView.delegate = self;
    [self.moreMainDetailsTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.moreMainDetailsTableView registerClass:[JSMLeavingMoreDetailsTableViewCell class] forCellReuseIdentifier:@"cellReuse"];
}
- (void)createTableHeaderView {
    BaseView *headerview = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 120)];
    BaseImageView *photoView = [[BaseImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - SCREENWIDTH / 6, 10, SCREENWIDTH / 6, SCREENWIDTH / 6)];
    photoView.layer.borderColor = [[UIColor orangeColor] CGColor];
    photoView.layer.cornerRadius = photoView.frame.size.height / 2;
    photoView.layer.masksToBounds = YES;
    
    BaseLabel *desLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, photoView.frame.size.height + 10, SCREENWIDTH - 10, SCREENWIDTH / 7)];
    [photoView sd_setImageWithURL:[NSURL URLWithString:self.mainModel.useModel.avatar_m] placeholderImage:PlaceHolderImage];
    BaseLabel *nameLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2, photoView.frame.size.height / 2 + 10 - SCREENWIDTH / 24, (SCREENWIDTH - 30) / 2, SCREENWIDTH / 12)];
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.textColor = [UIColor orangeColor];
    nameLabel.text = self.mainModel.useModel.name;
    desLabel.text = self.mainModel.index_title;
    desLabel.textColor = [UIColor orangeColor];
    
    [headerview addSubview:photoView];
    [headerview addSubview:nameLabel];
    [headerview addSubview:desLabel];
    self.moreMainDetailsTableView.tableHeaderView = headerview;
}

- (void) createData {
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot?spot_id=%@", self.spot_id];
    [AFNetworkingTool getUrl:str hudSuperView:self.view result:JSON success:^(id result) {
        if (result != nil) {
            self.moreDetailsMarr = [NSMutableArray array];
            self.recommendationsMarr = [NSMutableArray array];
            self.commendMarr = [NSMutableArray array];
            self.moreDetailsMarr = [JSMLeavingMoreMainDetailsModel baseModelByArr:result[@"data"][@"spot"][@"detail_list"]];
            self.recommendationsMarr = [JSMLeavingUserModel baseModelByArr:result[@"data"][@"spot"][@"recommendations"]];
            self.commendMarr = [JSMLeavingMoreDetailsCommentsModel baseModelByArr:result[@"data"][@"spot"][@"comments"]];
            if (self.moreDetailsMarr.count > 0) {
                [self.moreMainDetailsTableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        JSMLeavingMoreDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellReuse" forIndexPath:indexPath];
        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.moreDetailsMarr[indexPath.row] photo_s]] placeholderImage:PlaceHolderImage];
        cell.mainLabel.text = [self.moreDetailsMarr[indexPath.row] textStr];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[[self.commendMarr[indexPath.row] useModel] avatar_s]] placeholderImage:PlaceHolderImage];
       
        cell.imageView.layer.borderWidth = 1;
        cell.imageView.layer.borderColor = [[UIColor orangeColor] CGColor];
        cell.imageView.layer.cornerRadius = cell.imageView.frame.size.height / 2;
        cell.imageView.layer.masksToBounds = YES;
        cell.textLabel.text = [self.commendMarr[indexPath.row] comment];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        [cell.textLabel sizeToFit];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.numberOfLines = 0;
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.moreDetailsMarr.count;
    } else {
        return self.commendMarr.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 80;
    } else {
        return 0;
    }
}
#pragma mark tableView的返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CGFloat imageHeight = (SCREENWIDTH - 30) * 2 / 3 + 10 + 10;
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
        CGRect rect = [[self.moreDetailsMarr[indexPath.row] textStr] boundingRectWithSize:CGSizeMake(SCREENWIDTH - 30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return imageHeight + rect.size.height;
    } else {
        return 50;
    }
}
#pragma mark tableView的headerSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        BaseView *headerView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
        BaseLabel *titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 30, 30)];
        [headerView addSubview:titleLabel];
        titleLabel.text = self.mainModel.textStr;
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.numberOfLines = 0;
        return headerView;
    } else {
        BaseView *headerView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
        headerView.backgroundColor = [UIColor whiteColor];
        BaseLabel *titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 30, 30)];
        [headerView addSubview:titleLabel];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = [NSString stringWithFormat:@"%ld人评论", self.commendMarr.count];
        titleLabel.textColor = [UIColor orangeColor];
        titleLabel.numberOfLines = 0;
        return headerView;
    }
}
#pragma mark footView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        BaseView *footerView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 80)];
        BaseLabel *likeLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 30, SCREENWIDTH / 15)];
        likeLabel.text = [NSString stringWithFormat:@"%ld人👍", self.recommendationsMarr.count];
        [footerView addSubview:likeLabel];
        likeLabel.textColor = [UIColor orangeColor];
        likeLabel.font = [UIFont systemFontOfSize:15];
        BaseLabel *lineLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, likeLabel.frame.size.height, SCREENWIDTH - 30, 1)];
        lineLabel.layer.borderColor = [[UIColor colorWithRed:0.922 green:0.922 blue:0.945 alpha:1.000] CGColor];
        [footerView addSubview:lineLabel];
        
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
        BaseCollectionView *footerCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(15, 40, SCREENWIDTH - 30, SCREENWIDTH / 11) collectionViewLayout:flowlayout];
        footerCollectionView.dataSource = self;
        footerCollectionView.delegate = self;
        flowlayout.itemSize = CGSizeMake(SCREENWIDTH / 11, SCREENWIDTH / 11);
        footerCollectionView.showsHorizontalScrollIndicator = NO;
        [footerView addSubview:footerCollectionView];
        [footerCollectionView registerClass:[JSMLeavingMoreDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"footerCollectionView"];
        return footerView;
        
    } else {
        return nil;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.recommendationsMarr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMLeavingMoreDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"footerCollectionView" forIndexPath:indexPath];
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.recommendationsMarr[indexPath.row] avatar_s]] placeholderImage:PlaceHolderImage];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
